//
//  WmcSelectWaterMarkVC.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/31.
//

import UIKit

// MARK: - 扩展WmcSelectWatermarkVC + WmcNavigatorProtocol

extension WmcSelectWatermarkVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.selectWatermark]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        var vm = WmcSelectWatermarkVM()
        if let viewModel = parms.context as? WmcSelectWatermarkVM {
            vm = viewModel
        }
        let vc = WmcSelectWatermarkVC(vm)
        return vc
    }
}

// MARK: - 扩展WmcSelectWatermarkVC + WmcVMProtocol

extension WmcSelectWatermarkVC: WmcVMProtocol {
    typealias VMType = WmcSelectWatermarkVM
}

// MARK: - [WmcSelectWatermarkVC]

class WmcSelectWatermarkVC: WmcBaseVC {
    // Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()
        
    }

    // Internal

    lazy var layout: UICollectionViewFlowLayout = {
        let layout = UICollectionViewFlowLayout
            .YP.CreateGridLayout(scrollDirection: .vertical,
                                 minimumLineSpacing: 8,
                                 minimumInteritemSpacing: 8,
                                 sectionInset: Wmc_standardEdge)
        return layout
    }()

    lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView
            .YP.Create(layout: layout,
                       delegate: self,
                       dataSource: self,
                       registers: [
                           YPRegister(WmcSelectWatermarkCell.self),
                       ])
        collectionView.alwaysBounceVertical = true
        return collectionView
    }()
    
    private lazy var sureBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setBackgroundImage(UIColor.wmc_mainTheme.yp.uiImage, for: .normal)
        btn.setTitle("确定", for: .normal)
        btn.setTitleColor(.wmc_white_FFFFFF, for: .normal)
        btn.titleLabel?.font = 14.wmc_font
        btn.layer.cornerRadius = 4
        btn.layer.masksToBounds = true
        return btn
    }()

    override func makeUI() {
        makeNavView(showShadow: false)
        title = "选择水印"
        
        navView.addSubview(sureBtn)
        addSubview(collectionView)
        
        sureBtn.snp.makeConstraints { make in
            make.centerY.equalTo(navView.titleLabel)
            make.width.equalTo(52)
            make.height.equalTo(32)
            make.right.equalTo(-16)
        }
        
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }

    override func bindViewModel() {
        
    }
    
    override func bindViewAction() {
        sureBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.viewModel.ensure()
            weakSelf.navigationController?.popViewController(animated: true)
        }
    }
}

// MARK: - UICollectionViewDelegate

extension WmcSelectWatermarkVC: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.row < viewModel.watermarks.count {
            let model = viewModel.watermarks[indexPath.row]
            model.isSelected = !model.isSelected
            viewModel.watermarks[indexPath.row] = model
        }
        
        collectionView.reloadData()
    }
}

// MARK: - UICollectionViewDelegateFlowLayout

extension WmcSelectWatermarkVC: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let lineCount: CGFloat = 2
        let lineSpacing = self.layout.minimumLineSpacing * (lineCount - 1)
        let horizontalEdge = self.layout.sectionInset.yp.horizontal
        let width = (collectionView.frame.size.width - lineSpacing - horizontalEdge) / lineCount - 1
        let height = 148.0 / 172 * width
        return CGSize(width: width, height: height)
    }
}

// MARK: - UICollectionViewDataSource

extension WmcSelectWatermarkVC: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel.watermarks.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: WmcSelectWatermarkCell = collectionView.yp.dequeueReusableCell(for: indexPath)
        if let model = viewModel.watermarks.yp.object(at: indexPath.row) {
            cell.configModel(model)
        }
        return cell
    }
}
